#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit")
HotKeySet("9", "theExit")

call("screenshot", "start")

$numsweeps = IniRead("settings.ini", "run", "numsweeps", 1)

$stormarmor = 1 ;storm armor is slot 1
$explosion = 1 ;archon explosion is slot 1
$teleport = 2 ;teleport is slot 2
$magicweapon = 3 ;magic weapon is slot 3
$familiar = 4 ;familiar is slot 4
$goldpiles = 5 ;Number of gold piles to pick up

$chestnum = 0;how many chests found
$chestlegnum = 0;how many legendaries we found in chest


While 1 
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repairadventure") ;F4
	processkey('74', "findring") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
	processkey('79', "getGems") ;F10
WEnd

Func leorics()	
While 1
	Sleep($lagfactor)
	
	;Log Run Start
	call("logmessage", "Starting Run " & $runs)
	$runs = $runs + 1
	call("screenshot", "heartbeat"&$runs)

	;Randomly take a break
	If $runs > 30 Then
		call("takebreak")
	EndIf
	
	;Buff
	Local $mode = Random(1,6,1)
	If $mode = 1 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 2 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	ElseIf $mode = 3 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 4 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	ElseIf $mode = 5 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	Else
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	EndIf

	Sleep(Random(600,800,1))
	
	;Repair if needed
	call("repairadventure")
	
	;open map and select kulle
	Send("m")
	Sleep(Random(200,500,1))
	call("fuzzyclick", "right", 1088, 517)
	Sleep(Random(400,600,1))
	call("fuzzyclick", "left", 1088, 517)
	Sleep(Random(400,600,1))
	call("fuzzyclick", "left", 883, 281)
	Sleep(Random(2000,2200,1))
	Sleep($lagfactor)
	
	;Close Objectives
	call("fuzzyclick","left",1878,379,5)
	
	;teleport to portal
	call("fuzzymove", 1670, 16)
	Sleep(Random(400,600,1))
	Send("2")
	Sleep(Random(400,500,1))
	Send("2")
	Sleep(Random(400,500,1))
	Send("2")
	Sleep(Random(400,500,1))
	
	Sleep(Random(400,600,1))
	
	;Click to enter portal
	call("fuzzyclick", "left", 1000,550, 5)
	Sleep(Random(1500,2000,1) + $lagfactor)
	
	;Walk up to kulle and trigger cutscene
	;Buff
	Local $mode = Random(1,2,1)
	If $mode = 1 Then
		call("fuzzyclick", "left", 965, 15)
		Sleep(Random(2200,2400,1))
		call("fuzzyclick", "left", 1556, 99)
		Sleep(Random(4400,4600,1))
	
		;Wait for kulle to TP on top
		Sleep(Random(1000,1200,1))
	

		;Move mouse left of ourselves
		call("fuzzymove", 834, 500)
	
		;Fire left
		call("fuzzyclick", "right", 881,482)
	Else
		call("fuzzyclick", "left", 1901, 432)
		Sleep(Random(2200,2400,1))
		call("fuzzyclick", "left", 1551, 88)
		Sleep(Random(4400,4600,1))
	
		;Wait for kulle to TP on top
		Sleep(Random(1000,1200,1))
	

		;Move mouse left of ourselves
		call("fuzzymove", 834, 500)
	
		;Fire left
		call("fuzzyclick", "right", 881,482)
	EndIf
	
	
	$kulletimes = 0
	While KulleLives() = 1
		$kulletimes = $kulletimes + 1
		
		If $kulletimes > 20 Then
			ExitLoop
		EndIf
		
		If $kulletimes = 3 OR $kulletimes = 15 Then
			Send($potionbutton)
			Sleep(Random(200,500,1))
		EndIf
		
		Send("{SHIFTDOWN}")	
		MouseDown("left")
		call("fuzzymove", Random(834,1108,1),Random(367,595,1))
		call("fuzzysleep", Random(0,200,1))
		
		If $kulletimes < 10 Then
			call("castSpell", $explosion)
		EndIf
		
		call("fuzzysleep", Random(2000,2200,1))
		MouseUp("left")
		Sleep(Random(100,400,1))
		Send("{SHIFTUP}")
		Sleep(Random(100,400,1))
	WEnd
	
	Sleep(Random(1500,1700,1))
	
	call("screenshot","kill")
	
	Send("{NUMPADSUB}")
	Sleep(Random(100,200,1))
	
	;Pick up Legendaries
	call("getLegendaries", 0)
	Sleep(500)
	
	;Pick up Gems
	call("getGems", 0)
	Sleep(500)
	
	;Pick up gold
	call("pickupGold", 1)
	
	call("exitgame")
	
	;Restart Script
WEnd	
	
EndFunc

Func KulleLives()
	$kullecolor = 0xA335EE
	
	$coord = PixelSearch(834,23,1078,53,$kullecolor, 5)
	If IsArray($coord) Then
		Return 1
	EndIf
	
	Return 0
EndFunc